/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.v1.jitclouds.mda.providers;

import br.ufba.lasid.v1.jitclouds.mda.MDA;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.BasicCluster;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.ICluster;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.virtualization.INode;
import java.util.List;
import java.util.Properties;
import org.apache.commons.configuration.PropertiesConfiguration;

/**
 *
 * @author aliriosa
 */
public class ClusterProvider implements IProvider<ICluster> {

    @Override
    public ICluster create(Properties options) throws Exception {
        
        BasicCluster cluster = new BasicCluster(); 
        
        String idCluster = MDA.BaseOptions.getProperty(MDA.ClusterTag);
        String dsCluster = MDA.Configuration.getString(idCluster + ".description");
        
        cluster.setID(idCluster);
        cluster.setDescription(dsCluster);
        
        List nodesID = MDA.Configuration.getList(idCluster + ".node.id");
        
        MDA.debug("Loading the nodes of cluster " + cluster.getID() + "...");
        
        for(Object nodeID : nodesID){
            MDA.BaseOptions.setProperty(MDA.NodeTag, (String)nodeID);
            
            INode node = MDA.createNode(options);
            
            cluster.addNode(node);
            
        }
        
        MDA.debug(cluster.size() + " node(s) were loaded in " + cluster.getID() + "."); 
        MDA.debug("The cluster " + cluster.getID() + "[" + cluster.getDescription() + "] has been successfully loaded.");                    
                       
        return cluster;
        
    }
    
}
